local nianying1 = fk.CreateSkill {

  name = "joy__nianying1",

  tags = {  },

}



nianying1:addEffect(fk.TurnStart, {
  name = "joy__nianying1",
    anim_type = "special",
    events = {fk.TurnStart,fk.TurnEnd},
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if event == fk.TurnStart then
            for _, p in ipairs(room.alive_players) do
                if p.kingdom ~= "shu" then
                    return false
                end
            end
            return target == player and  player:hasSkill(nianying1.name) 
        elseif event == fk.TurnEnd then
            if player:hasSkill(nianying1.name) then
                local m = player:getMark("joy__nianying1-turn") or 0
                local n = 0
                for _, p in ipairs(room.alive_players) do
                    if p.kingdom == "shu" then
                        n = n + 1
                    end
                end
                return n ~= m
            end
        end
    end,
    on_cost = function(self, event, target, player, data)
      return player.room:askForSkillInvoke(player,nianying1.name,nil,"念影：你可以摸两张牌并执行一项“念影”效果。")
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      if not player.dead then
        player:drawCards(2,nianying1.name)
      end
      if player.dead then return end
      nianying(player)
    end,
})
nianying1:addEffect(fk.TurnEnd, {
  name = "joy__nianying1",
    anim_type = "special",
    events = {fk.TurnStart,fk.TurnEnd},
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if event == fk.TurnStart then
            for _, p in ipairs(room.alive_players) do
                if p.kingdom ~= "shu" then
                    return false
                end
            end
            return target == player and  player:hasSkill(nianying1.name) 
        elseif event == fk.TurnEnd then
            if player:hasSkill(nianying1.name) then
                local m = player:getMark("joy__nianying1-turn") or 0
                local n = 0
                for _, p in ipairs(room.alive_players) do
                    if p.kingdom == "shu" then
                        n = n + 1
                    end
                end
                return n ~= m
            end
        end
    end,
    on_cost = function(self, event, target, player, data)
      return player.room:askForSkillInvoke(player,nianying1.name,nil,"念影：你可以摸两张牌并执行一项“念影”效果。")
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      if not player.dead then
        player:drawCards(2,nianying1.name)
      end
      if player.dead then return end
      nianying(player)
    end,
})
nianying1:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart},
    can_refresh = function(self, event, target, player, data)
        return  player:hasSkill(nianying1.name)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local n = 0
        for _, p in ipairs(room.alive_players) do
            if p.kingdom == "shu" then
                n = n + 1
            end
        end
        room:setPlayerMark(player,"joy__nianying1-turn",n)
    end,
})

return nianying1